package com.ns.scs.rsr.service;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ns.core.current.config.SymbolsMark;
import com.ns.core.utils.StpUtil;
import com.ns.scs.collections.mapper.CollectionsEntryMapper;
import com.ns.scs.collections.vo.CollectionsVO;
import com.ns.scs.rsr.dto.RsrPageDto;
import com.ns.scs.rsr.entry.RsrEntry;
import com.ns.scs.rsr.mapper.RsrEntryMapper;
import com.ns.scs.rsr.vo.ResourcesExtraVO;
import com.ns.scs.rsr.vo.ResourcesVO;
import com.ns.scs.tags.entry.TagsEntry;
import com.ns.scs.tags.mapper.TagsEntryMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 参数业务实现
 *
 * @author lw
 * @since 2024/09/23
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RsrServiceImpl extends ServiceImpl<RsrEntryMapper, RsrEntry> implements IRsrService {
    private final RsrEntryMapper mapper;
    private final TagsEntryMapper tagsEntryMapper;
    private final CollectionsEntryMapper collectionsMapper;

    @Override
    public Page<ResourcesExtraVO> getList(RsrPageDto dto) {
        return mapper.getList(dto, new Page<>(dto.getCurrent(), dto.getSize()));
    }

    @Override
    public List<ResourcesExtraVO> getSearch(RsrPageDto dto) {
        // 查询
        return mapper.getSearch(dto);
    }

    @Override
    public ResourcesVO getResourceById(Long id) {

        ResourcesExtraVO extraVO = mapper.getResourceDetailById(id);
        if (Objects.isNull(extraVO)) {
            return null;
        }
        // 封装
        ResourcesVO resourcesVO = new ResourcesVO();
        BeanUtils.copyProperties(extraVO, resourcesVO);

        // 查询收藏状态
        List<CollectionsVO> collections = collectionsMapper.getByResourceIdAndUidAndType(id, StpUtil.getLoginUserId(), SymbolsMark.ONE_S);
        resourcesVO.setCollectFlag(!ObjectUtils.isEmpty(collections));
        if (!StringUtils.hasText(resourcesVO.getTags())) {
            return resourcesVO;
        }

        // 组装tags
        List<Long> params = new ArrayList<>();
        for (Object param : JSON.parseArray(resourcesVO.getTags())) {
            params.add(Long.parseLong(String.valueOf(param)));
        }

        List<TagsEntry> tagsEntries = tagsEntryMapper.selectBatchIds(params);
        if (CollectionUtils.isEmpty(tagsEntries)) {
            return resourcesVO;
        }

        List<String> tags = new ArrayList<>();
        for (TagsEntry tagsEntry : tagsEntries) {
            tags.add(tagsEntry.getName());
        }
        resourcesVO.setTags(String.join(SymbolsMark.COMMA_1, tags));
        return resourcesVO;
    }

    @Override
    public Long getMaxSort() {
        return mapper.getMaxSort();
    }

    @Override
    public ResourcesExtraVO getSourceDetailById(Long id) {
        return mapper.getResourceDetailById(id);
    }
}
